package com.wan.service.bill;

import com.wan.dao.BaseDao;
import com.wan.dao.bill.BillDao;
import com.wan.dao.bill.BillDaoImpl;
import com.wan.pojo.Bill;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class BillServiceImpl implements BillService{
    BillDao billDao;
    public BillServiceImpl () {
        billDao = new BillDaoImpl();
    }

    @Override
    public int getBillCount(String productName, int providerId,int isPayment) {
        Connection conn = null;
        int count = 0;
        try {
            conn = BaseDao.getConnection();
            count = billDao.getBillCount(conn,productName,providerId,isPayment);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return count;
    }

    @Override
    public List<Bill> getBillList(String productName, int providerId, int isPayment, int currentPageNo, int pageSize) {
        Connection conn = null;
        ArrayList<Bill> billArrayList = null;
        try {
            conn = BaseDao.getConnection();
            billArrayList = (ArrayList<Bill>) billDao.getBillList(conn, productName, providerId, isPayment,currentPageNo, pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return billArrayList;
    }

    @Override
    public List<Integer> getProviderId() {
        Connection conn = null;
        ArrayList<Integer> providerList = null;
        int count = 0;
        try {
            conn = BaseDao.getConnection();
            providerList = (ArrayList<Integer>) billDao.getProviderId(conn);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return providerList;
    }

    @Override
    public int insertBill(Bill bill,int createdBy) {
        Connection conn = null;
        int impactedCount = 0;
        int count = 0;
        try {
            conn = BaseDao.getConnection();
            impactedCount = billDao.insertBill(conn,bill,createdBy);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return impactedCount;
    }

    @Override
    public int deleteBill(int id) {
        Connection conn = null;
        int impactedCount = 0;
        int count = 0;
        try {
            conn = BaseDao.getConnection();
            impactedCount = billDao.deleteBill(conn,id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return impactedCount;
    }

    @Override
    public Bill getBillById(int id) {
        Connection conn = null;
        Bill bill = null;
        int count = 0;
        try {
            conn = BaseDao.getConnection();
            bill = billDao.getBillById(conn,id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return bill;
    }

    @Override
    public int updateBill(Bill bill, int modifyBy) {
        Connection conn = null;
        int impactedCount = 0;
        int count = 0;
        try {
            conn = BaseDao.getConnection();
            impactedCount = billDao.updateBill(conn,bill,modifyBy);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return impactedCount;
    }
}
